<?php
namespace app\zyapi\controller;

/**
 * @Author: user
 * @Date:   2019-01-18 13:24:23
 * @Last Modified by:   user
 * @Last Modified time: 2019-01-23 10:00:37
 */
use think\Image;
use think\Controller;
use think\Loader;//引入此文件
//require APP_PATH."zyapi/lib/zy_image/phpqrcode/phpqrcode/phpqrcode.php";
/**
 * 图片生成类
 */
class ImageController extends Controller
{
    const PATH =  'http://oa.300c.cn';
		
	/**
   *生成二维码方法
   *@method scerweima()
   *@param  $url  传递需要生成的url
   *@return 
   *            成功返回：string  图片字符流
   *            错误返回：['status'=>'error','message'=>'缺少必要的参数 url']
   *@example  html调用显示
   *            <img src="http://oa.300c.cn/zyapi/image/scerweima?url=www.zy.com" alt="二维码" title="二维码"> 
   */
	public function scerweima(){
	    Loader::import('phpqrcode', __DIR__."/../lib/zy_image/phpqrcode", '.php');
        $data=$this->request->param(); 
        if(!isset($data['url'])){
          return json_encode(['status'=>'error','message'=>'缺少必要的参数 url']);
        }
        $url=$data['url'];
        $url = urldecode($url);
        $object = new \QRcode();
        ob_clean();//这个一定要加上，清除缓冲区
        $object->png($url,false,'Q','6','2');
	}

  /**
   *生成一维码方法
   *@method barcode()
   *@param  $num  传递需要生成的num字符
   *@return 
   *            成功返回：string  图片字符流
   *            错误返回：['status'=>'error','message'=>'缺少必要的参数 num']
   *@example  html调用显示
   *            <img src="http://oa.300c.cn/zyapi/image/barcode?num=888888" alt="二维码" title="二维码"> 
   */ 
	 public function barcode(){
     $data=$this->request->param();
     if(!isset($data['num'])){
         return json_encode(['status'=>'error','message'=>'缺少必要的参数 num']);
     }
     Loader::import('code', __DIR__."/../lib/zy_barcodegen", '.php');
     $code= new \code();
     $code->s($data['num']);
  } 

   public function upload(){
    // 获取表单上传文件 例如上传了001.jpg
	
    $files = request()->file('image'); //获取上传的图片文件
	
	if(!$files){  //判断是否存在文件上传
		  echo json_encode(['status'=>"error",'msg'=>'No_Found_File']);
	      exit();
	}
   $patharr = [];
   foreach($files as $file){  //多图上传
    if (true !== $this->validate(['image' => $file], ['image' => 'require|image'],['size'=>2097152])) {
         echo json_encode(['status'=>"error"]);
	     exit();
    } else {
     // 读取图片
     $image = Image::open($file);
	 $type = request()->param("type"); //水印:1 压缩图片:2
	
	 switch ($type) {
	   case 1:
		  $text = request()->param("text");  
	      $image->text($text,'SIMKAI.TTF', 15, '#000000'); //水印
	   break;
	   case 2:
	    $image->thumb(150, 150, Image::THUMB_CENTER);  //缩略图
	   break;
	 }
	 
	 $saveName = request()->time() . '.png';  //文件名字
	 $image->save('upload/compressimg/' . $saveName);  //存储路径
	 array_push($patharr,self::PATH.'/upload/compressimg/' . $saveName);

     }
   }
    echo json_encode(['status'=>"success","path"=>json_encode($patharr)]);
	exit();
 }

}
